package main

import "fmt"

// 迭代器模式用于遍历集合中的元素，无论集合的数据结构是怎样的
type collection interface {
	createIterator() iterator
}
type part struct {
	title  string
	number int
}
type partCollection struct {
	// part
	parts []*part
}

func (u *partCollection) createIterator() iterator {
	return &partIterator{
		parts: u.parts,
	}
}

type iterator interface {
	hasNext() bool
	getNext() *part
}
type partIterator struct {
	index int
	parts []*part
}

func (u *partIterator) hasNext() bool {
	return u.index < len(u.parts)
}
func (u *partIterator) getNext() *part {
	if u.hasNext() {
		part := u.parts[u.index]
		u.index++
		return part
	}
	return nil
}

func ExampleIterator() {
	part1 := &part{
		title:  "part1",
		number: 10,
	}
	part2 := &part{
		title:  "part2",
		number: 20,
	}
	part3 := &part{
		title:  "part3",
		number: 30,
	}
	partCollection := &partCollection{
		parts: []*part{part1, part2, part3},
	}
	iterator := partCollection.createIterator()
	for iterator.hasNext() {
		part := iterator.getNext()
		fmt.Println(part)
	}
}
